/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Qizx.Xml;
using QName = System.Qizx.Api.QName;
using NamespaceContext = System.Qizx.Util.NamespaceContext;
namespace System.Qizx.Xdm
{
	
	/// <summary> Manages a list of attributes in the way of SAX,</summary>
	public class AttributeList : SaxAttributesSupport
	{
		virtual public int AttributeCount
		{
			get
			{
				return attrCnt;
			}
			
		}
		virtual public int NamespaceCount
		{
			// ---------------------------------------------------------------------
			
			
			get
			{
				return nsContext.LocalSize;
			}
			
		}
		virtual public NamespaceContext NamespaceContext
		{
			get
			{
				return nsContext;
			}
			
		}
		protected internal IQName[] attrNames; // must be interned
		protected internal System.String[] attrValues;
		protected internal System.String[] attrTypes;
		protected internal int attrCnt;
		protected internal NamespaceContext nsContext = new NamespaceContext();
		
		public AttributeList()
		{
		}
		
		public virtual void  reset()
		{
			attrCnt = 0;
		}
		
		public virtual bool addAttribute(QName name, string value_Renamed, string type)
		{
			// check duplicates
			for (int a = 0; a < attrCnt; a++)
				if (attrNames[a] == name)
				{
					attrValues[a] = value_Renamed;
					return false; // not added
				}
			rawAddAttribute(name, value_Renamed, type);
			return true;
		}
		
		protected internal virtual void  rawAddAttribute(QName name, string value_Renamed, string type)
		{
			if (attrNames == null)
				return ;
			if (attrCnt >= attrNames.Length)
			{
				QName[] oldn = attrNames;
				attrNames = new IQName[oldn.Length * 2];
				Array.Copy(oldn, 0, attrNames, 0, oldn.Length);
				System.String[] oldv = attrValues;
				attrValues = new System.String[attrNames.Length];
				Array.Copy(oldv, 0, attrValues, 0, oldv.Length);
				oldv = attrTypes;
				attrTypes = new System.String[attrNames.Length];
				Array.Copy(oldv, 0, attrTypes, 0, oldv.Length);
			}
			attrNames[attrCnt] = IQName.GetQName(name);
			attrValues[attrCnt] = value_Renamed;
			attrTypes[attrCnt] = type;
			++attrCnt;
		}
		
		// --------------- implement sax Attributes -------------------------------
		
		public virtual int getIndex(QName qName)
		{
			for (int i = attrCnt; --i >= 0; )
				if (attrNames[i] == qName)
					return i;
			return - 1;
		}
		
		public override int GetIndex(string qName)
		{
			return getIndex(nsContext.expandName(qName));
		}
		
		public override int GetIndex(string uri, string localName)
		{
			return getIndex(IQName.GetQName(uri, localName));
		}
		
		public override int GetLength()
		{
			return attrCnt;
		}
		
		public override string GetLocalName(int index)
		{
			return (index < 0 || index >= attrCnt)?null:attrNames[index].Name;
		}
		
		public override string GetURI(int index)
		{
			return (index < 0 || index >= attrCnt)?null:attrNames[index].Namespace;
		}
		
		public override string GetFullName(int index)
		{
			return (index < 0 || index >= attrCnt)?null:nsContext.prefixedName(attrNames[index]);
		}
		
		public override string GetType(int index)
		{
			string type = (attrTypes == null || index < 0 || index >= attrCnt)?null:attrTypes[index];
			return type == null?"CDATA":type;
		}
		
		public override string GetType(string qName)
		{
			return "CDATA";
		}
		
		public override string GetType(string uri, string localName)
		{
			return "CDATA";
		}
		
		public override string GetValue(int index)
		{
			return (index < 0 || index >= attrCnt)?null:attrValues[index];
		}
		
		public override string GetValue(string qName)
		{
			return GetValue(GetIndex(qName));
		}
		
		public override string GetValue(string uri, string localName)
		{
			return GetValue(GetIndex(uri, localName));
		}
		
		public virtual string getNamespacePrefix(int index)
		{
			return nsContext.getLocalPrefix(index);
		}
		
		public virtual string getNamespaceURI(int index)
		{
			return nsContext.getLocalNamespaceURI(index);
		}
	}
}